<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            background: url(../images/2.jpg);
        }
        
        div {
            width: 200px;
            height: 40px;
            margin: 0 auto;
        }
        
        li {
            list-style: none;
            float: left;
        }
        
        img {
            width: 50px;
        }
    </style>
</head>

<body>
    <div>
        <ul class="baidu">
            <li><img src="../images/2.jpg"></li>
            <li><img src="../images/1.jpg"></li>
            <li><img src="../images/3.jpg"></li>
        </ul>
    </div>
    <script>
        //1.获取元素
        var imgs = document.querySelector(".baidu").querySelectorAll("img");
        //console.log(imgs);
        //2.循环注册事件
        for (i = 0; i < imgs.length; i++) {
            imgs[i].onclick = function() {
                //console.log(this.src);
                //this.src是当前选中的图片的路径
                document.body.style.backgroundImage = 'url(' + this.src + ')';
            }

        }
    </script>
</body>

</html>